<template>
  <div class="whiteBox">
    <el-form :style="{ height: tableHeight + 'px' }" style="padding: 20px;overflow: auto">
      <div v-if="dataList.length !== 0" style="padding-bottom: 10px">共{{ total || 0 }}条记录</div>
      <div v-show="dataList.length !== 0" style="overflow:auto" :style="{ height: tableHeight - 70 + 'px' }">
        <el-timeline v-infinite-scroll="load" infinite-scroll-disabled="disabled" infinite-scroll-distance="50" infinite-scroll-immediate="immediate">
          <el-timeline-item v-for="item in dataList" :key="item.id" hide-timestamp placement="top">
            <div>
              <b>{{ item.serviceName }}</b>
            </div>
            <div class="timeline-text">
              <span>{{ item.createTime }}</span>
              <span>操作人：{{ item.userName }}</span>
            </div>
            <div class="timeinfo">
              <el-row class="content">
                <el-col v-for="(col, cindex) in columns" :key="cindex" :xs="24" :sm="col.col" :md="col.col" :lg="col.col" :xl="col.col">
                  <FormComponents :item-obj="col" :form="item" :isedit="false">
                    <template slot="remark">
                      <div style="white-space: pre-wrap; margin: 10px;line-height: 1.75" v-html="item.remark"></div>
                    </template>
                  </FormComponents>
                </el-col>
              </el-row>
            </div>
          </el-timeline-item>
          <p v-if="loading" style="text-align: center;">加载中...</p>
          <p v-if="noMore && !loading" style="text-align: center;">没有更多了</p>
        </el-timeline>
      </div>

      <div v-if="dataList.length === 0">
        <div style="margin: 0; text-align: center;height: 200px;line-height:200px">暂无数据</div>
      </div>
    </el-form>
  </div>
</template>
<script>
import dayjs from 'dayjs'
import FormComponents from '@/components/FormComponents/index.vue'
// import { baseLogList } from '@/views/PurchaseOrder/api/delivery'
export default {
  components: { FormComponents },
  props: {
    // 业务id
    serviceId: {
      type: [String, Number],
      default: ''
    },
    // 业务类型
    serviceType: {
      type: String,
      default: ''
    },
    // 是否立即执行加载方法，以防初始状态下内容无法撑满容器。
    immediate: {
      type: Boolean,
      default: true
    },
    // 日志展示区高度
    tableHeight: {
      type: Number,
      default: 310
    }
  },
  data() {
    return {
      dataList: [],
      columns: [
        { slot: 'remark', col: 24 },
        { prop: 'fileList', label: '', type: 'upload', col: 24, component: 'upload' }
      ],
      count: 0,
      total: 10,
      loading: false,
      params: {
        serviceId: this.serviceId,
        serviceType: this.serviceType,
        pageSize: 10,
        pageNum: 1
      }
    }
  },
  computed: {
    noMore() {
      return this.count >= this.total
    },
    disabled() {
      return this.loading || this.noMore
    }
  },
  methods: {
    load() {
      this.loading = true
      this.getData()
    },
    getList() {
      this.getData()
    },
    getData() {
      baseLogList(this.params)
        .then(res => {
          let list = res.data.list.map(r => {
            if (r.remark) {
              r.remark = r.remark.replace(/\r\n/g, '</br>').replace(/=>/g, ' 改为 ')
            } else {
              r.remark = '暂无'
            }
            r.createTime = dayjs(r.createTime).format('YYYY-MM-DD HH:mm:ss')
            return r
          })
          if (this.params.pageNum !== 1) {
            list = [...this.dataList, ...list]
          }
          this.dataList = list
          this.total = res.data.total
          this.count = this.dataList.length

          if (this.count < this.total) {
            this.params.pageNum++
          }

          setTimeout(() => {
            this.loading = false
          }, 1000)
        })
        .catch(e => {
          this.loading = false
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.timeline-text {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
}
.content {
  background: #f4f5f7;
  border-radius: 10px;
}
</style>
